<?php
declare (strict_types=1);

namespace app\admin\model\admin;

use app\admin\model\dev\Action;
use app\admin\model\dev\Menu;
use app\BaseModel;
use app\common\Constants;
use think\facade\Db;
use think\facade\Log;
use think\Model;

/**
 * @mixin \think\Model
 */
class Role extends BaseRole
{


    /**
     * 修改器：角色的 Code 统一为大写字母
     * @param $value
     * @return string
     */
    public function setCodeAttr($value): string
    {
        return strtoupper($value);
    }

    /**
     * 一个角色可分配给多个用户
     * @return \think\model\relation\BelongsToMany
     */
    public function users(): \think\model\relation\BelongsToMany
    {
        return $this->belongsToMany(User::class, 'admin_role_user', 'user_id', 'role_id');
    }

    /**
     * @return \think\model\relation\BelongsToMany
     */
    public function menus(): \think\model\relation\BelongsToMany
    {
        return $this->belongsToMany(Menu::class, 'admin_role_menu', 'menu_id', 'role_id');
    }

    /**
     * @return \think\model\relation\BelongsToMany
     */
    public function actions(): \think\model\relation\BelongsToMany
    {
        return $this->belongsToMany(Action::class, 'admin_role_action', 'action_id', 'role_id');
    }
}
